package com.jd.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jd.dao.MoneyRewardDao;
import com.jd.dao.NameDao;
import com.jd.dao.RulesDao;
import com.jd.dao.TicketDao;
import com.jd.dao.UserDao;
import com.jd.entity.MoneyRewardEntity;
import com.jd.entity.MyTicketEntity;
import com.jd.entity.NameEntity;
import com.jd.entity.PromotingWalletsEntity;
import com.jd.entity.RealnameEntity;
import com.jd.entity.RulesEntity;
import com.jd.entity.UserEntity;
import com.jd.service.UserService;
import com.jd.util.IdUtil;
@Service
public class UserImpl implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private MoneyRewardDao moneyRewardDao;
	@Autowired
	private TicketDao ticketDao;
	@Autowired
	private RulesDao rulesDao;
	@Autowired
	private NameDao nameDao;
	
	@Override
	public HashMap<String, Object> getUserRole(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return userDao.getUserRole(map);
	}

	@Override
	public boolean inRealName(RealnameEntity realnameEntity) {
		// TODO Auto-generated method stub
		return userDao.inRealName(realnameEntity);
	}

	@Override
	public RealnameEntity getRealName(String userId) {
		// TODO Auto-generated method stub
		return userDao.getRealName(userId);
	}

	@Override
	public HashMap<String, Object> getUser(String userId) {
		// TODO Auto-generated method stub
		return userDao.getUser(userId);
	}

	@Override
	public Boolean updateBalance(HashMap<String, Object> hashMap) {
		// TODO Auto-generated method stub
		return userDao.updateBalance(hashMap);
	}

	@Override
	public Boolean updateZFB(HashMap<String, Object> zFBMap) {
		// TODO Auto-generated method stub
		return userDao.updateZFB(zFBMap);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getOnUserList(String userId) {
		// TODO Auto-generated method stub
		return userDao.getOnUserList(userId);
	}

	@Override
	public Boolean updateMBalance(HashMap<String, Object> hashMap) {
		// TODO Auto-generated method stub
		return userDao.updateMBalance(hashMap);
	}

	@Override
	public Boolean extension(UserEntity user) {
		// TODO Auto-generated method stub
		return userDao.extension(user);
	}

	@Override
	public UserEntity userByOpenId(String openId) {
		// TODO Auto-generated method stub
		return userDao.userByOpenId(openId);
	}

	@Override
	public Boolean updateUserLogo(UserEntity user) {
		// TODO Auto-generated method stub
		return userDao.updateUserLogo(user);
	}

	@Override
	public Boolean insertUser(UserEntity userEntity) {
		// TODO Auto-generated method stub
		Boolean u = userDao.insertUser(userEntity);
		if(u){
			RulesEntity rules = rulesDao.getRules();
			String term = rules.getTerm();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			System.out.println("当前日期:"+sf.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, Integer.parseInt(term));
			Date time = c.getTime();
			MyTicketEntity myTicketEntity = new MyTicketEntity();
			ArrayList<Map<String,String>> nameList = nameDao.getALLGameList();
			if(nameList.size()!=0 && nameList!=null){
				for (Map<String, String> map : nameList) {
					String id = map.get("id").toString();
					String idByUUId = IdUtil.getIdByUUId();
					myTicketEntity.setId(idByUUId);
					myTicketEntity.setUserId(userEntity.getId());
					myTicketEntity.setNameId(id);
					myTicketEntity.setAccess("1");
					myTicketEntity.setUserStart("1");
					myTicketEntity.setGetTime(new Date());
					myTicketEntity.setOutTime(time);
					myTicketEntity.setCreateBy(userEntity.getId());
					myTicketEntity.setCreateTime(new Date());
					myTicketEntity.setDelflag("0");
					ticketDao.save(myTicketEntity);
			}
			}
			
			//初始化钱包
			MoneyRewardEntity moneyRewardEntity = new MoneyRewardEntity();
			moneyRewardEntity.setId(IdUtil.getIdByUUId());
			moneyRewardEntity.setUserId(userEntity.getId());
			moneyRewardEntity.setBalance(BigDecimal.valueOf(0));
			moneyRewardEntity.setAppreciating(BigDecimal.valueOf(0));
			moneyRewardEntity.setAccumulatedAmount(BigDecimal.valueOf(0));
			moneyRewardEntity.setCreateBy(userEntity.getId());
			moneyRewardEntity.setCreateTime(new Date());
			moneyRewardEntity.setDelflag("0");
			moneyRewardDao.save(moneyRewardEntity);
			//初始化推广钱包
			PromotingWalletsEntity promotingWalletsEntity = new PromotingWalletsEntity();
			promotingWalletsEntity.setId(IdUtil.getIdByUUId());
			promotingWalletsEntity.setUserId(userEntity.getId());
			promotingWalletsEntity.setBalance(BigDecimal.valueOf(0));
			promotingWalletsEntity.setAppreciating(BigDecimal.valueOf(0));
			promotingWalletsEntity.setAccumulatedAmount(BigDecimal.valueOf(0));
			promotingWalletsEntity.setCreateBy(userEntity.getId());
			promotingWalletsEntity.setCreateTime(new Date());
			promotingWalletsEntity.setDelflag("0");
			moneyRewardDao.saveWallets(promotingWalletsEntity);
			////初始化认证
			RealnameEntity realnameEntity = new RealnameEntity();
			realnameEntity.setId(IdUtil.getIdByUUId());
			realnameEntity.setUserId(userEntity.getId());
			realnameEntity.setState("0");
			realnameEntity.setCreateBy(userEntity.getId());
			realnameEntity.setCreateTime(new Date());
			realnameEntity.setDelflag("0");
			moneyRewardDao.saveRealName(realnameEntity);
			//判断是否有上级
			if(userEntity.getOnUserId()!="" && userEntity.getOnUserId()!=null ){
				HashMap<String, Object> user = userDao.getUser(userEntity.getOnUserId());
				//判断上级是否是平台推广员
				if(user.get("identity").equals("3")){
					Integer count = userDao.countOnUser(userEntity.getOnUserId());
					HashMap<String, String> hashMap = new HashMap<String, String>();
					hashMap.put("id", userEntity.getOnUserId());
					if(count<=10){
						hashMap.put("rank", "0");
						userDao.updateRank(hashMap);
					}
					if(count>10 && count<=50){
						hashMap.put("rank", "1");
						userDao.updateRank(hashMap);
					}
					if(count>50 && count<=100){
						hashMap.put("rank", "2");
						userDao.updateRank(hashMap);
					}
					if(count>100 && count<=200){
						hashMap.put("rank", "3");
						userDao.updateRank(hashMap);
					}
					if(count>200){
						hashMap.put("rank", "4");
						userDao.updateRank(hashMap);
					}
				}
			}
		}
		return u;
	}

	@Override
	public UserEntity getUserByPhone(String phone) {
		// TODO Auto-generated method stub
		return userDao.getUserByPhone(phone);
	}

	@Override
	public Integer updateOpenId(HashMap<String, String> phoneMap) {
		// TODO Auto-generated method stub
		return userDao.updateOpenId(phoneMap);
	}

	@Override
	public String selectOnUserId(String id) {
		// TODO Auto-generated method stub
		return userDao.selectOnUserId(id);
	}

	@Override
	public Boolean updateTBalance(HashMap<String, Object> balanceMap) {
		// TODO Auto-generated method stub
		return userDao.updateTBalance(balanceMap);
	}

	@Override
	public HashMap<String, Object> getUserCode(String onUserId1) {
		// TODO Auto-generated method stub
		return userDao.getUserCode(onUserId1);
	}

	@Override
	public Boolean updateTotalBalance(BigDecimal bfb, String userId) {
		// TODO Auto-generated method stub
		return userDao.updateTotalBalance(bfb,userId);
	}
}
